{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/wtf-langchain/blob/main/02_Models/Models.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "F3PJ9w-JGMbL"
      },
      "outputs": [],
      "source": [
        "# openai需要指定旧版本，否则会报 module 'openai' has no attribute 'error'\n",
        "!pip install langchain==0.0.235 openai==0.28.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-p0HnkaSbBSe",
        "outputId": "3db21660-db1d-4a1b-8f64-dd92cf6d3dd2"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "\n",
            "AI (Artificial Intelligence) is the ability of a computer or machine to think and learn, and to act independently of humans. It includes the use of algorithms, data analysis, and robotics to carry out complex tasks that would normally require human intelligence. AI can be used to automate mundane tasks, improve decision-making processes, and create new products and services.\n"
          ]
        }
      ],
      "source": [
        "from langchain.llms import OpenAI\n",
        "\n",
        "import os\n",
        "os.environ['OPENAI_API_KEY'] = '您的有效OpenAI API Key'\n",
        "\n",
        "# text-davinci-003模型已经弃用，使用gpt-3.5-turbo进行测试\n",
        "llm = OpenAI(model_name=\"gpt-3.5-turbo\")\n",
        "response = llm.predict(\"What is AI?\")\n",
        "print(response)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7t2nQzTEGXy1",
        "outputId": "addb81a7-e130-4bc7-dfa3-51e43197dbf6"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "content='AI, or Artificial Intelligence, refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It involves the development of computer systems capable of performing tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, problem-solving, and language translation. AI can be categorized into two types: Narrow AI, which is designed for specific tasks, and General AI, which possesses the ability to understand, learn, and apply knowledge across various domains.' additional_kwargs={} example=False\n"
          ]
        }
      ],
      "source": [
        "from langchain.chat_models import ChatOpenAI\n",
        "from langchain.schema import AIMessage, HumanMessage, SystemMessage\n",
        "\n",
        "import os\n",
        "os.environ['OPENAI_API_KEY'] = '您的有效OpenAI API Key'\n",
        "\n",
        "chat = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo')\n",
        "response = chat.predict_messages([\n",
        "  HumanMessage(content=\"What is AI?\")\n",
        "])\n",
        "print(response)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Z7En92dhwD0h",
        "outputId": "332773f9-0c22-40bf-9b7a-71d78cd18e68"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "langchain.schema.messages.AIMessage"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "response.__class__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ssIbQXyyq4Rk",
        "outputId": "e95da07f-e094-4b4b-cead-3f912f07a8ca"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "content=\"I'm sorry, but I don't know.\" additional_kwargs={} example=False\n"
          ]
        }
      ],
      "source": [
        "response = chat.predict_messages([\n",
        "  SystemMessage(content=\"You are a chatbot that knows nothing about AI. When you are asked about AI, you must say 'I don\\'t know'\"),\n",
        "  HumanMessage(content=\"What is deep learning?\")\n",
        "])\n",
        "print(response)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "authorship_tag": "ABX9TyNDb1VJ0bhh4X+zrRM1TDh8",
      "include_colab_link": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
